<?php
/*
 * @Author: zeyudada
 * @Date: 2022-02-21 20:05:18
 * @LastEditTime: 2022-04-07 22:16:14
 * @Description: 导入北京东奥题目
 * @Q Q: zeyunb@vip.qq.com(1776299529)
 * @E-mail: admin@zeyudada.cn
 * 
 * Copyright (c) 2022 by zeyudada, All Rights Reserved. 
 */

require('include/common.php');

//题目文件
$file = file_get_contents('2022.txt');
$ques = explode("\n\n", $file);

// 因为后台答案保存的不是ABCD，所以需要替换下
function myans($ans)
{
    switch ($ans) {
        case 'A':
            return 'options1';
            break;
        case 'B':
            return 'options2';
            break;
        case 'C':
            return 'options3';
            break;
        case 'D':
            return 'options4';
            break;

        default:
            return false;
            break;
    }
}

// $str:原字符串，$suffix:子字符串（区分大小写）
function endWith($str, $suffix)
{   
    $length = strlen($suffix);
    if ($length == 0) {
        return true;
    }   
    return (substr($str, -$length) === $suffix);
} 


foreach ($ques as $value) {
    $que = explode("\n", $value);

    preg_match('/([0-9]{1,3})(、)([\x00-\xff]+)/', $que[0], $match);
    $here['num'] = $match[1];
    $here['title'] = $match[3];
    preg_match('/([A-D]{1,3})(、)([\x00-\xff]+)(\(正确答案\))?/', $que[1], $o1match);
    if(endWith($o1match[3],'(正确答案)')){
        $here['answer'] = 'options1';
        $here['options1'] = str_replace('(正确答案)','',$o1match[3]);
    } else $here['options1'] = $o1match[3];
    preg_match('/([A-D]{1,3})(、)([\x00-\xff]+)(\(正确答案\))?/', $que[2], $o2match);
    if(endWith($o2match[3],'(正确答案)')){
        $here['answer'] = 'options2';
        $here['options2'] = str_replace('(正确答案)','',$o2match[3]);
    } else $here['options2'] = $o2match[3];
    preg_match('/([A-D]{1,3})(、)([\x00-\xff]+)(\(正确答案\))?/', $que[3], $o3match);
    if(endWith($o3match[3],'(正确答案)')){
        $here['answer'] = 'options3';
        $here['options3'] = str_replace('(正确答案)','',$o3match[3]);
    } else $here['options3'] = $o3match[3];
    preg_match('/([A-D]{1,3})(、)([\x00-\xff]+)(\(正确答案\))?/', $que[4], $o4match);
    if(endWith($o4match[3],'(正确答案)')){
        $here['answer'] = 'options4';
        $here['options4'] = str_replace('(正确答案)','',$o4match[3]);
    } else $here['options4'] = $o4match[3];
/*
    $here['options2'] = $o2match[3];
    $here['options3'] = $o3match[3];
    $here['options4'] = $o4match[3];
*/
    $question[$here['num']] = $here;

    //var_dump($here);
}

var_dump($question);

# 导入进数据库
foreach ($question as $insert) {
    //var_dump($insert);
    //if ($DB->query("INSERT INTO `question`(`sort`, `title`, `options1`, `options2`, `options3`, `options4`, `answer`, `parse`) VALUES (2, '{$insert['title']}', '{$insert['options1']}', '{$insert['options2']}', '{$insert['options3']}', '{$insert['options4']}', '{$insert['answer']}', '{$insert['parse']}')") < 1) var_dump($insert);
}
